﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public delegate void MyEventHandlerCtlMembers(object sender, int iValue);

public partial class UserControls_ctlMembers : System.Web.UI.UserControl
{

    public event MyEventHandlerCtlMembers Allocate;
    public event MyEventHandlerCtlMembers Deallocate;

    private DataTable dtMembers
    {
        get
        {
            return (DataTable)(Session["dtMembers"]);
        }
        set
        {
            (Session["dtMembers"]) = value;
        }
    }

    public bool Enabled
    {
        get
        {
            return (ctlComboBox.Enabled || btnAssign.Enabled || GridView.Enabled);
        }
        set
        {
            ctlComboBox.Enabled = value;
            btnAssign.Enabled = value;
            GridView.Enabled = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Cargar combobox
        if (!IsPostBack)
        {
            CtrlBDHumanResource ctrlBDHumanResource = new CtrlBDHumanResource();

            ctlComboBox.setDataSource(ctrlBDHumanResource.getData(), "Name", "Id");
            dtMembers = new DataTable();

            dtMembers.Columns.Add("Id", Type.GetType("System.String"));
            dtMembers.Columns.Add("Name", Type.GetType("System.String"));
        }
            GridView.DataSource = dtMembers;
        
        if (!IsPostBack)
        {
            GridView.DataBind();
        }

    }

    protected void GridView_RowCommand(object source, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView.Rows[index];
        TableCell cellId = selectedRow.Cells[0];

        if (e.CommandName == "Deallocate")
        {
            deallocateMember(cellId.Text); 
        }
    }
    
    protected void btnAssign_Click(object sender, EventArgs e)
    {
        allocateMember(ctlComboBox.getSelectedId(), ctlComboBox.getSelectedName());
    }

    public void deallocateMember(string sId)
    {
        for (int i = 0; i < dtMembers.Rows.Count; ++i)
        {
            if (dtMembers.Rows[i]["Id"].Equals(sId))
            {
                ctlMessage1.showMessage("warning", "Se ha desasignado a " + dtMembers.Rows[i]["Name"] + ".");
                dtMembers.Rows.RemoveAt(i);
            }
        }

        GridView.DataBind();

        if (Deallocate != null)
        {
            Deallocate(this, int.Parse(sId));
        }
    }

    public void allocateMember(string sId, string sName)
    {
        bool bExist = false;

        for (int i = 0; i < dtMembers.Rows.Count; ++i)
        {
            if (dtMembers.Rows[i]["Id"].Equals(sId))
            {
                bExist = true;
            }
        }

        if (!bExist)
        {
            dtMembers.Rows.Add();
            dtMembers.Rows[dtMembers.Rows.Count - 1]["Id"] = sId;
            dtMembers.Rows[dtMembers.Rows.Count - 1]["Name"] = sName;

            ctlMessage1.showMessage("info", "Se ha asignado a " + sName);

            GridView.DataBind();

            if (Allocate != null)
            {
                Allocate(this, int.Parse(sId));
            }
        }
    }

    public void loadMembers(string sId, string sName)
    {
        //cargar miembros de lista ternaria
        dtMembers.Rows.Add();
        dtMembers.Rows[dtMembers.Rows.Count - 1]["Id"] = sId;
        dtMembers.Rows[dtMembers.Rows.Count - 1]["Name"] = sName;
    }

    public void clearMembers()
    {
        dtMembers.Clear();
    }

    public void hideButtons()
    {
        GridView.Columns[2].Visible = false;
    }

    public void showButtons()
    {
        GridView.Columns[2].Visible = true;
    }

}
